/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.xd.controller;

import com.xy.biz.xd.client.TechniqueClient;
import com.xy.biz.xd.domain.converter.TechniqueConverter;
import com.xy.biz.xd.domain.query.TechniqueQuery;
import com.xy.biz.xd.domain.req.GetTechniqueRequest;
import com.xy.biz.xd.domain.req.ListTechniqueRequest;
import com.xy.biz.xd.domain.req.SaveTechniqueRequest;
import com.xy.biz.xd.domain.resp.GetTechniqueResponse;
import com.xy.biz.xd.service.TechniqueService;
import com.xy.lang.domain.KeyValue;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.enums.ApplyStatusEnum;
import com.xy.lang.enums.AuditStatusEnum;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 *
 *
 * @author zgy
 * @date 2022-1-11
 */
@Api(tags = "生产工艺服务")
@Slf4j
@RestController
@RequestMapping("/api/xd/technique/")
public class TechniqueController implements TechniqueClient {

    @Autowired
    private TechniqueService techniqueService;

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveTechnique(@RequestBody SaveTechniqueRequest request) {
        return techniqueService.saveTechnique(TechniqueConverter.toBO(request));
    }

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllTechnique(@RequestBody Collection<SaveTechniqueRequest> collection) {
        techniqueService.saveAllTechnique(TechniqueConverter.toBO(collection));
        return Result.success();
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateTechnique(@RequestBody SaveTechniqueRequest request) {
        return techniqueService.updateTechnique(TechniqueConverter.toBO(request));
    }

    @ApiOperation("复制")
    @PostMapping(value = "/copy")
    public Result<?> copyTechnique(@RequestBody GetTechniqueRequest request) {
        return techniqueService.copyTechnique(request.getTechniqueId());
    }

    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteTechnique(@RequestBody GetTechniqueRequest request) {
        return techniqueService.deleteTechnique(request.getTechniqueId());
    }

    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetTechniqueResponse> getTechnique(@RequestBody GetTechniqueRequest request) {
        return Result.success(TechniqueConverter.toResponse(techniqueService.getTechnique(TechniqueConverter.toQuery(request))));
    }

    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetTechniqueResponse>> listTechniqueById(@RequestBody Collection<Long> collection) {
        return Result.success(TechniqueConverter.toResponse(techniqueService.listTechniqueById(collection)));
    }

    @PostMapping(value = "/list")
    @Override
    public Result<List<GetTechniqueResponse>> listTechnique(@RequestBody ListTechniqueRequest request) {
        return Result.success(TechniqueConverter.toResponse(techniqueService.listTechnique(TechniqueConverter.toQuery(request))));
    }

    @PostMapping(value = "/count")
    @Override
    public Result<Integer> countTechnique(@RequestBody ListTechniqueRequest request) {
        return Result.success(techniqueService.countTechnique(TechniqueConverter.toQuery(request)));
    }

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetTechniqueResponse>> pageTechnique(@RequestBody ListTechniqueRequest request) {
        TechniqueQuery query = TechniqueConverter.toQuery(request);
        List<GetTechniqueResponse> list = TechniqueConverter.toResponse(techniqueService.listTechnique(query));
        int count = techniqueService.countTechnique(query);
        return Result.success(new Page<>(list, count));
    }

    @GetMapping(value = "/list_pre_options")
    public Result<Map<String, List>> listPreOptions() {
        Map<String, List> map = new HashMap<>();
        map.put("status", listStatus());
        map.put("auditStatus", listStatus1());
        return Result.success(map);
    }

    private List<KeyValue<Integer, String>> listStatus() {
        ApplyStatusEnum[] enums = ApplyStatusEnum.values();
        List<KeyValue<Integer, String>> list = new ArrayList<>();
        for (ApplyStatusEnum e : enums) {
            list.add(e.toKeyValue());
        }
        return list;
    }

    private List<KeyValue<Integer, String>> listStatus1() {
        AuditStatusEnum[] enums = AuditStatusEnum.values();
        List<KeyValue<Integer, String>> list = new ArrayList<>();
        for (AuditStatusEnum e : enums) {
            list.add(e.toKeyValue());
        }
        return list;
    }
}
